package com.zzh.mycalldemo

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.telephony.SubscriptionManager
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {

    private val mDualSimTypes by lazy {
        arrayOf(
            "extra_asus_dial_use_dualsim",
            "com.android.phone.extra.slot",
            "slot",
            "simslot",
            "sim_slot",
            "subscription",
            "Subscription",
            "phone",
            "com.android.phone.DialingMode",
            "simSlot",
            "slot_id",
            "simId",
            "simnum",
            "phone_type",
            "slotId",
            "slotIdx"
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val onClickListener = View.OnClickListener {
            if (ContextCompat.checkSelfPermission(
                    this,
                    Manifest.permission.CALL_PHONE
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                Toast.makeText(this, "请手动开启拨打电话的权限", Toast.LENGTH_LONG).show()
                return@OnClickListener
            }

            val slotIndex = it.tag as Int
            val phone = getPhone()

            if (checkPhone(phone)) {
                startActivity(Intent(Intent.ACTION_CALL).apply {
                    data = Uri.parse("tel:$phone")
                    mDualSimTypes.forEach { type ->
                        putExtra(type, slotIndex)
                    }
                })
                return@OnClickListener
            }

            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_LONG).show()
        }

        findViewById<View>(R.id.call1).apply {
            tag = getSubId(0)
            setOnClickListener(onClickListener)
        }

        findViewById<View>(R.id.call2).apply {
            tag = getSubId(1)
            setOnClickListener(onClickListener)
        }

    }

    private fun getSubId(slotIndex: Int): Int {
        val subscriptionManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
        } else {
            null
        }
        val subscriptionIds = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            subscriptionManager?.getSubscriptionIds(slotIndex)
        } else {
            null
        }
        return subscriptionIds?.get(0) ?: 0
    }

    private fun checkPhone(phone: String): Boolean = phone.length == 11

    private fun getPhone(): String = findViewById<EditText>(R.id.phone).text.toString()
}